package org.robot.work;

import java.io.File;

import org.robot.Spider;
import org.robot.component.Page;
import org.robot.dao.AuthorDao;
import org.robot.dao.IssueDao;
import org.robot.dao.JournalDao;
import org.robot.dao.PaperDao;
import org.robot.pojo.Author;
import org.robot.pojo.Issue;
import org.robot.pojo.Journal;
import org.robot.pojo.Paper;
import org.robot.utils.HttpDownload;
import org.robot.utils.MD5;
import org.robot.utils.MatchEmail;
import org.robot.utils.RegexUtil;

public class esimeg extends Spider {

	private static final Long TASKID = 636L;

	private void parsePaper(Page page) {
		String title = "";
		try {
			title = page.select("span#lblname").text();
		} catch (Exception e) {
			title = "";
		}

		System.out.println("文章标题： " + title + page.url());
		// 摘要
		String abst = page.select("div.box").text();
		String doi=page.select("span#lbldoi").text();
//
		Long issueId = IssueDao.selectPidByWebsite(Issue.class, page.prevUrl(),TASKID);
		if (issueId == null)
			return;
		Paper paper = new Paper();
		paper.setTitle(title);
		paper.setAbstracts(abst);
//		paper.setKeywords(importance);
		paper.setPdfUrl(page.url() + "#");
		paper.setIssueId(issueId);
		paper.setDoi(doi);
		paper.setTaskid(TASKID);
		paper.setWebsite(page.url());
		boolean success = PaperDao.addPaper(paper);
		if (success) {
			System.out.println("添加paper成功--" + issueId);
			parseAuthor(page);
		}
	}

	private void parseAuthor(Page page) {
		Long paperId = PaperDao.selectPidByWebsite(Paper.class, page.url(),TASKID);
		
		//取出pdf链接
		String pdfStr=page.select("a#hrfpdf").attr("href");
		HttpDownload.saveContent(pdfStr, "utf-8", "E:/webSpider/jaypeejournals/pdfDownload", MD5.getMD5(page.url()) + ".pdf");
		
		//从下载的PDF中获取email								
		String emails = MatchEmail.getEmailString(new File("E:/webSpider/jaypeejournals/pdfDownload", MD5.getMD5(page.url()) + ".pdf"));
		
		String[] em =emails.split("\\^");

	    String authors=page.select("span#lblauthor").text();
		
		authors = authors.replace("*", "").replace("\\d+", "");
		
		String[] authorname = authors.split("and|，|,|;|；|&");

		for (int i = 0; i < authorname.length; i++) {
			String author = authorname[i].trim();
			String email = "";
			boolean flag = true;
			for (int j = 0; j < em.length; j++) {
				if (flag) {
					String name = MatchEmail.similarName(authorname, em[j]);
					if (name.contains(author)) {
						email = em[j];
						flag = false;
					}
				}
			}
			if (!author.isEmpty()) {
				System.out.println("作者：" + author + "\n邮箱：" + email);
				Author author2 = new Author();
				author2.setFullName(author);
				author2.setEmail(email);
				author2.setPaperid(paperId);
				author2.setTaskId(TASKID);
				boolean b = AuthorDao.addAuthor(author2);
				if (b) {
					System.out.println("author爬取成功！");
				} else {
					System.out.println("author爬取失败！" + page.url());
				}
			}
		}

	}

	private void parseIssue(Page page) {
		String issueValue = page.select("span#lblissue").text().trim();
		System.out.println("issue: " + page.url() + " --  " + issueValue + " -- " + issueValue + " -- " + page.prevUrl());
		String year=RegexUtil.regexFirst("\\d{4}", issueValue);
		Long journalId = JournalDao.selectPidByWebsite(Journal.class, "http://www.jaypeejournals.com/eJournals/IssueDetails.aspx?Value=19",TASKID);
		if (journalId == null) {
			return;
		}
		Issue issue = new Issue();
		issue.setIssue(issueValue);
		
		issue.setJournalId(journalId);
		issue.setYear(year);
		issue.setWebsite(page.url());
		issue.setTaskId(TASKID);
		IssueDao.addIssue(issue);
	}

	private void parseJournal(Page page) {
		Journal journal = new Journal();
//		String issn = page.select("dl:contains(ISSN)").text();
//		issn = RegexUtil.regexFirst("\\w{4}-\\w{4}", issn);
//		String journalName = page.select("title").text();
		journal.setJournalname("THE DUKE ORTHOPAEDIC JOURNAL");
		journal.setIssn("2231-5055");
		journal.setWebsite(page.url());
		journal.setTaskId(TASKID);
		JournalDao.addJournal(journal);
	}

	public static void main(String[] args) {
		esimeg claw = new esimeg();
		claw.setBaseDirectory("E:/webSpider/jaypeejournals");
		claw.addSeed("http://www.jaypeejournals.com/eJournals/IssueDetails.aspx?Value=19");
//		claw.addRegex("http://www.jaypeejournals.com/eJournals/IssueDetails.aspx\\?Value=19","journal");
		claw.addRegex("http://www.jaypeejournals.com/eJournals/TopicDetails.aspx\\?id=.+AID=19", "issue");
		claw.addRule("a.search-link",".+", "paper");

		claw.start();
	}

	@Override
	public void parse(Page page) throws Exception {
		
		if (page.isSeed()) {
			parseJournal(page);
		}
		if (page.typeEquals("issue")) {
			parseIssue(page);
		}
		if (page.typeEquals("paper")) {
			parsePaper(page);
		}
	}

}
